<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="身份证" prop="idCard">
        <el-input v-model="dataForm.idCard" placeholder="身份证"></el-input>
      </el-form-item>
      <el-form-item label="证书编号" prop="certificateNo">
        <el-input v-model="dataForm.certificateNo" placeholder="证书编号"></el-input>
      </el-form-item>
      <el-form-item label="分类" prop="categoryId">
        <el-select v-model="dataForm.categoryId" placeholder="分类" :rules="[{ required: true, message: '请选择分类', trigger: 'blur' }]">
          <el-option v-for="item in categoryList" :key="item.id" :label="item.categoryName" :value="parseInt(item.id)"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="证书" prop="img">
        <el-upload style="border: 1px dashed #d9d9d9; border-radius: 6px" class="avatar-uploader" :action="img" :show-file-list="false" :on-success="handleHeaderSuccess" :before-upload="beforeHeaderUpload">
          <img v-if="dataForm.img" :src="dataForm.img" class="avatar" />
          <el-icon v-else class="el-icon--upload" style="width: 200px"><upload-filled /></el-icon>
        </el-upload>
        <!-- <el-input v-model="dataForm.header" placeholder="一寸证件照"></el-input> -->
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, reactive } from "vue";
import baseService from "@/service/baseService";
import { useDebounce } from "@/utils/utils";
import { IObject } from "@/types/interface";
import { getToken } from "@/utils/cache";
import app from "@/constants/app";
export default defineComponent({
  setup() {
    return reactive({
      visible: false,
      dataForm: {
        id: "",
        name: "",
        idCard: "",
        certificateNo: "",
        categoryName: "",
        createDate: "",
        expirationDate: "",
        img: "",
        categoryId: ""
      },
      categoryList: [],
      img: ""
    });
  },
  computed: {
    dataRule() {
      return {};
    }
  },
  created() {
    this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);

    this.img = `${app.api}/sys/oss/upload?token=${getToken()}`;
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        baseService.get("/nuaa/tcategory/list").then((response) => {
          this.categoryList = response.data;
        });
        this.$refs["dataFormRef"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      baseService.get("/nuaa/tcertificate/" + this.dataForm.id).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.dataForm = res.data;
      });
    },
    // 表单提交
    dataFormSubmitHandle() {
      this.$refs["dataFormRef"].validate((valid: boolean) => {
        if (!valid) {
          return false;
        }
        (!this.dataForm.id ? baseService.post : baseService.put)("/nuaa/tcertificate", this.dataForm).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        });
      });
    },
    handleHeaderSuccess(res: IObject, file: IObject) {
      this.dataForm.img = res.data.location;
    },
    beforeHeaderUpload(file: IObject) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 4;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 4MB!");
      }
      return isJPG && isLt2M;
    }
  }
});
</script>
